﻿using System;

namespace DBMock.Builders
{
    /// <summary>
    /// 时间builder
    /// </summary>
    public class DateTimeBuilder : Builder
    {
        private DateTime min; //最小值
        private int maxSpan; //最大间隔

        /// <summary>
        /// 初始化DateTimeBuilder
        /// </summary>
        /// <param name="seed">随机数种子</param>
        /// <param name="minTime">最小值</param>
        /// <param name="maxTime">最大值</param>
        public DateTimeBuilder(int seed, DateTime minTime, DateTime maxTime)
            : base(seed)
        {
            if (minTime < new DateTime(1753, 1, 1))
            { throw new Exception("Minimum cannot be smaller than 1753-1-1."); }

            if (maxTime > new DateTime(9999, 12, 31))
            { throw new Exception("Maximum cannot be larger than 9999-12-31."); }

            if (minTime > maxTime)
            { throw new Exception("Minimum is larger than maximum."); }

            var ts = new TimeSpan(maxTime.Ticks - minTime.Ticks);

            if (ts.TotalSeconds > int.MaxValue)
            { throw new Exception("Time span cannot be larger than 2147483647 seconds(about 90 years)."); }

            min = minTime;
            maxSpan = (int)ts.TotalSeconds;
        }

        public override object Build()
        {
            int span = random.Next(maxSpan);
            return min.AddSeconds(span);
        }
    }
}
